---
title: "Integrate Summarizer Pipe in Node.js"
description: "Learn how to integrate the summarizer pipe in your Node.js project."
tags:
    - baseai
    - langbase
    - learn
    - pipe
section: "nodejs"
published: 2024-09-24
modified: 2024-09-24
---

# Integrate Summarizer Pipe in Node.js

### Learn how to integrate the summarizer pipe in your Node.js project

<Note sub="/learn">
This guide is part of the /learn BaseAI course. For context, [start from the beginning](/learn) to follow along.
</Note>

---

In this learn guide, we will:

- **Create** a typescript file to integrate the summarizer pipe.
- **Import** the summarizer pipe in the typescript file.
- **Update** env variables with OpenAI API key.

---

## Step #1: Create a typescript file

Create an `index.ts` file in your project. You can use the following command:

```bash
touch index.ts
```

## Step #2: Import the summarizer pipe

In this `index.ts` file, import the summarizer pipe you created. We will use the Pipe primitive from `@baseai/core` to run the pipe.

Add the following code to `index.ts` file:

<CodeGroup exampleTitle="BaseAI Pipe & Tool" title="Run AI agent Pipe with tools">

```ts {{ title: 'index.ts' }}
import 'dotenv/config';
import { Pipe, getRunner } from '@baseai/core';
import pipeSummarizer from './baseai/pipes/summarizer';

const pipe = new Pipe(pipeSummarizer());

const userMsg = `
Langbase studio is your playground to build, collaborate, and deploy AI. It allows you to experiment with your pipes in real-time, with real data, store messages, version your prompts, and truly helps you take your idea from building prototypes to deployed in production with LLMOps on usage, cost, and quality.
A complete AI developers platform.
- Collaborate: Invite all team members to collaborate on the pipe. Build AI together.
- Developers & Stakeholders: All your R&D team, engineering, product, GTM (marketing and sales), literally invlove every stakeholder can collaborate on the same pipe. It's like a powerful version of GitHub x Google Docs for AI. A complete AI developers platform.
`;

async function main() {
	const { stream } = await pipe.run({
		messages: [{ role: 'user', content: userMsg }],
		stream: true
	});

	const runner = getRunner(stream);

	// Method 1: Using event listeners
	runner.on('connect', () => {
		console.log('Stream started.\n');
	});

	runner.on('content', content => {
		process.stdout.write(content);
	});

	runner.on('end', () => {
		console.log('\nStream ended.');
	});

	runner.on('error', error => {
		console.error('Error:', error);
	});
}

main();
```

```ts {{ title: './baseai/pipes/summarizer.ts' }}
import { PipeI } from '@baseai/core';

const pipeSummarizer = (): PipeI => ({
	apiKey: process.env.LANGBASE_API_KEY!, // Replace with your API key https://langbase.com/docs/api-reference/api-keys
	name: 'summarizer',
	description: 'A pipe that summarizes content and make it less wordy',
	status: 'public',
	model: 'openai:gpt-4o-mini',
	stream: true,
	json: false,
	store: true,
	moderate: true,
	top_p: 1,
	max_tokens: 1000,
	temperature: 0.7,
	presence_penalty: 1,
	frequency_penalty: 1,
	stop: [],
	tool_choice: 'auto',
	parallel_tool_calls: false,
	messages: [
		{
			role: 'system',
			content: `You are a content summarizer. You will summarize content without loosing context into less wordy to the point version.`
		}
	],
	variables: [],
	memory: [],
	tools: []
});

export default pipeSummarizer;
```

</CodeGroup>

We have added a user message to the pipe. We will use this message to test the pipe. We will also use the `pipe.run` method with `stream: true` to stream the text in the terminal.

## Step #3: Add OpenAI API key in env

Since we are using an OpenAI model, your OpenAI key is required. Please create an `.env` file in the root directory (if there isn't already one present) using the following command:

```bash
touch .env
```

Once you have created the `.env` file, add your OpenAI key to it.

```txt
OPENAI_API_KEY="<REPLACE-OPENAI-KEY>" # Add your OpenAI API key in .env file
```

---

_In the next learn guide, we will run the AI pipe we created._

---
